package com.kotlin.android.router.factory

import com.alibaba.android.arouter.launcher.ARouter
import com.kotlin.android.ktx.utils.LogUtils
import com.kotlin.android.router.provider.IBaseProvider

/**
 * 创建者: zl
 * 创建时间: 2020/6/8 9:43 AM
 * 描述: 实例化provider，提供调用入口
 */
object ProviderFactory {

    fun <T : IBaseProvider> createProvider(path: String, clazz: Class<T>): T? {
        var provider: T? = null
        try {
            provider = ARouter.getInstance().build(path).navigation() as T
        } catch (e: ClassNotFoundException) {
            LogUtils.e(e.toString())
        } catch (e: ClassCastException) {
            LogUtils.e(e.toString())
        } catch (e: InstantiationException) {
            LogUtils.e(e.toString())
        } catch (e: IllegalAccessException) {
            LogUtils.e(e.toString())
        } finally {
            return provider
        }
    }


    fun <T : IBaseProvider> getInstance(path: String): T? {
        var provider: T? = null
        try {
            provider = ARouter.getInstance().build(path).navigation() as T
        } catch (e: InstantiationException) {
            LogUtils.e(e.toString())
        } catch (e: IllegalAccessException) {
            LogUtils.e(e.toString())
        } catch (e: ClassCastException) {
            LogUtils.e(e.toString())
        } catch (e: ClassNotFoundException) {
            LogUtils.e(e.toString())
        } finally {
            return provider
        }
    }

}